SlideShare a Scribd company logo
Class No.39  Data Structures http://ecomputernotes.com
Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 http://ecomputernotes.com
Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 http://ecomputernotes.com
Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 http://ecomputernotes.com
Analysis To sort the halves  ( n /2) 2 +( n /2) 2 To merge the two halves    n   So, for  n =100, divide and conquer takes: = (100/2) 2  + (100/2) 2  + 100  = 2500 + 2500 + 100  = 5100 ( n 2  = 10,000)  http://ecomputernotes.com
Divide and Conquer Why not divide the halves in half? The quarters in half? And so on . . . When should we stop? At  n  = 1 http://ecomputernotes.com
Search Divide and Conquer Search Search Recall:  Binary Search http://ecomputernotes.com
Sort Divide and Conquer Sort Sort Sort Sort Sort Sort http://ecomputernotes.com
Divide and Conquer Combine Combine Combine http://ecomputernotes.com
Mergesort Mergesort  is a  divide and conquer  algorithm that does exactly that. It splits the list in half Mergesorts the two halves Then merges the two sorted halves together Mergesort can be implemented  recursively http://ecomputernotes.com
Mergesort The mergesort algorithm involves three steps: If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 4 http://ecomputernotes.com
Merging: animation 4 8 10 12 2 5 7 11 2 4 5 http://ecomputernotes.com
Merging 4 8 10 12 2 5 7 11 2 4 5 7 http://ecomputernotes.com
Mergesort 8 12 11 2 7 5 4 10 Split the list in half. 8 12 4 10 Mergesort the left half. Split the list in half. Mergesort the left half. 4 10 Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 http://ecomputernotes.com
Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 12 8 Merge the two halves . 8 8 12 http://ecomputernotes.com
Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 Merge the two halves. 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 10 12 8 4 10 4 8 12 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 11 2 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 2 11 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 http://ecomputernotes.com
Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 http://ecomputernotes.com
Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. http://ecomputernotes.com
Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. http://ecomputernotes.com
void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size];  if (tmpArrayPtr != NULL)  mergeSortRec(array, size, tmpArrayPtr); else { cout << “Not enough memory to sort list.\n”); return; } delete [] tmpArrayPtr; } Mergesort http://ecomputernotes.com
void mergeSortRec(int array[],int size,int tmp[]) { int i; int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp);  for (i = 0; i < size; i++)  array[i] = tmp[i]; } } Mergesort http://ecomputernotes.com
3 5 15 28 30 6 10 14 22 43 50 a: b: aSize: 5 bSize: 6 mergeArrays tmp: http://ecomputernotes.com
mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6 http://ecomputernotes.com
mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6 http://ecomputernotes.com
mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6 http://ecomputernotes.com
mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6 http://ecomputernotes.com
mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10 http://ecomputernotes.com
mergeArrays 10 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14 http://ecomputernotes.com
mergeArrays 14 10 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15 http://ecomputernotes.com
mergeArrays 14 10 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28 http://ecomputernotes.com
mergeArrays 14 10 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22 http://ecomputernotes.com
mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28 http://ecomputernotes.com
mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done. http://ecomputernotes.com
Merge Sort and Linked Lists Sort Sort Merge http://ecomputernotes.com
Mergesort Analysis Merging the two lists of size  n /2: O( n ) Merging the four lists of size n/4: O( n ) . . . Merging the n lists of size 1: O( n ) O (lg  n ) times Mergesort is O( n  lg  n ) Space? The other sorts we have looked at (insertion, selection) are  in-place  (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging
Mergesort Analysis Mergesort is O( n  lg  n ) Space? The other sorts we have looked at (insertion, selection) are  in-place  (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging http://ecomputernotes.com
Quicksort Quicksort   is another divide and conquer algorithm Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value http://ecomputernotes.com
Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or  middle  of list): 8 3 2 11 7 5 4 10 12 4 5 5 pivot value http://ecomputernotes.com
Quicksort The pivot is swapped to the last position and the  remaining elements are compared starting at the ends. 8 3 2 11 7 5 4 10 12 4 5 low high 5 pivot value http://ecomputernotes.com
Quicksort Then the low index moves right until it is at an element  that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 low high 5 pivot value 3 12 http://ecomputernotes.com
Quicksort Then the high index moves left until it is at an  element that is smaller than the pivot value (i.e., it  is on the wrong side) 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 http://ecomputernotes.com
Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 12 http://ecomputernotes.com
Quicksort This continues until the two index values pass  each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 3 10 3 10 http://ecomputernotes.com
Quicksort This continues until the two index values pass  each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 10 3 http://ecomputernotes.com
Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 low high 10 3 8 5 http://ecomputernotes.com
Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 Quicksort the left part Quicksort the right part 5 http://ecomputernotes.com
Quicksort void quickSort(int array[], int size) { int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } } http://ecomputernotes.com
int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid);  for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index; } Quicksort
Data Structures-Course Recap Arrays Link Lists Stacks Queues Binary Trees Sorting http://ecomputernotes.com

More Related Content

PPT
Computer notes - Mergesort
PPT
Computer notes - Analysis of Union
PPT
computer notes - Data Structures - 2
PPT
Computer notes - Josephus Problem
PPT
computer notes - Data Structures - 5
PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 15
PPT
Lec 6 Divide and conquer of Data Structures & Algortihms
Computer notes - Mergesort
Computer notes - Analysis of Union
computer notes - Data Structures - 2
Computer notes - Josephus Problem
computer notes - Data Structures - 5
computer notes - Data Structures - 30
computer notes - Data Structures - 15
Lec 6 Divide and conquer of Data Structures & Algortihms

Similar to computer notes - Data Structures - 39 (20)

PPT
Computer notes - Sorting
PPT
Computer notes data structures - 9
PPT
computer notes - Data Structures - 38
PDF
CS253: Divide & Conquer Sort (2019)
PPT
Computer notes - Maze Generator
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
PPTX
Merge sort analysis and its real time applications
PPT
computer notes - Data Structures - 33
PPT
Advanced s and s algorithm.ppt
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 31
PPT
16-sorting.ppt
PPT
PPTX
L2_DatabAlgorithm Basics with Design & Analysis.pptx
PPT
PPT
computer notes - Data Structures - 9
PPT
Computer notes - Build Heap
PDF
Chapter 8 advanced sorting and hashing for print
Computer notes - Sorting
Computer notes data structures - 9
computer notes - Data Structures - 38
CS253: Divide & Conquer Sort (2019)
Computer notes - Maze Generator
Analysis of Algorithm (Bubblesort and Quicksort)
Merge sort analysis and its real time applications
computer notes - Data Structures - 33
Advanced s and s algorithm.ppt
computer notes - Data Structures - 25
computer notes - Data Structures - 31
16-sorting.ppt
L2_DatabAlgorithm Basics with Design & Analysis.pptx
computer notes - Data Structures - 9
Computer notes - Build Heap
Chapter 8 advanced sorting and hashing for print
Ad

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 14
computer notes - Data Structures - 11
computer notes - Data Structures - 20
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 14
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Understanding_Digital_Forensics_Presentation.pptx
Spectroscopy.pptx food analysis technology
MYSQL Presentation for SQL database connectivity
Reach Out and Touch Someone: Haptics and Empathic Computing
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx

computer notes - Data Structures - 39

  • 1. Class No.39 Data Structures http://ecomputernotes.com
  • 2. Divide and Conquer What if we split the list into two parts? 10 12 8 4 2 11 7 5 10 12 8 4 2 11 7 5 http://ecomputernotes.com
  • 3. Divide and Conquer Sort the two parts: 10 12 8 4 2 11 7 5 4 8 10 12 2 5 7 11 http://ecomputernotes.com
  • 4. Divide and Conquer Then merge the two parts together: 4 8 10 12 2 5 7 11 2 4 5 7 8 10 11 12 http://ecomputernotes.com
  • 5. Analysis To sort the halves  ( n /2) 2 +( n /2) 2 To merge the two halves  n So, for n =100, divide and conquer takes: = (100/2) 2 + (100/2) 2 + 100 = 2500 + 2500 + 100 = 5100 ( n 2 = 10,000) http://ecomputernotes.com
  • 6. Divide and Conquer Why not divide the halves in half? The quarters in half? And so on . . . When should we stop? At n = 1 http://ecomputernotes.com
  • 7. Search Divide and Conquer Search Search Recall: Binary Search http://ecomputernotes.com
  • 8. Sort Divide and Conquer Sort Sort Sort Sort Sort Sort http://ecomputernotes.com
  • 9. Divide and Conquer Combine Combine Combine http://ecomputernotes.com
  • 10. Mergesort Mergesort is a divide and conquer algorithm that does exactly that. It splits the list in half Mergesorts the two halves Then merges the two sorted halves together Mergesort can be implemented recursively http://ecomputernotes.com
  • 11. Mergesort The mergesort algorithm involves three steps: If the number of items to sort is 0 or 1, return Recursively sort the first and second halves separately Merge the two sorted halves into a sorted group http://ecomputernotes.com
  • 12. Merging: animation 4 8 10 12 2 5 7 11 2 http://ecomputernotes.com
  • 13. Merging: animation 4 8 10 12 2 5 7 11 2 4 http://ecomputernotes.com
  • 14. Merging: animation 4 8 10 12 2 5 7 11 2 4 5 http://ecomputernotes.com
  • 15. Merging 4 8 10 12 2 5 7 11 2 4 5 7 http://ecomputernotes.com
  • 16. Mergesort 8 12 11 2 7 5 4 10 Split the list in half. 8 12 4 10 Mergesort the left half. Split the list in half. Mergesort the left half. 4 10 Split the list in half. Mergesort the left half. 10 Mergesort the right. 4 http://ecomputernotes.com
  • 17. Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 12 8 Merge the two halves . 8 8 12 http://ecomputernotes.com
  • 18. Mergesort 8 12 11 2 7 5 4 10 8 12 4 10 Merge the two halves. 4 10 Mergesort the right half. Merge the two halves. 10 4 8 12 10 12 8 4 10 4 8 12 http://ecomputernotes.com
  • 19. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 11 2 http://ecomputernotes.com
  • 20. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 11 2 2 11 2 11 http://ecomputernotes.com
  • 21. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 7 5 2 11 7 5 7 5 http://ecomputernotes.com
  • 22. Mergesort 10 12 11 2 7 5 8 4 Mergesort the right half. 11 2 5 7 2 11 http://ecomputernotes.com
  • 23. Mergesort 10 12 2 5 7 11 8 4 Mergesort the right half. http://ecomputernotes.com
  • 24. Mergesort 5 7 8 10 11 12 4 2 Merge the two halves. http://ecomputernotes.com
  • 25. void mergeSort(float array[], int size) { int* tmpArrayPtr = new int[size]; if (tmpArrayPtr != NULL) mergeSortRec(array, size, tmpArrayPtr); else { cout << “Not enough memory to sort list.\n”); return; } delete [] tmpArrayPtr; } Mergesort http://ecomputernotes.com
  • 26. void mergeSortRec(int array[],int size,int tmp[]) { int i; int mid = size/2; if (size > 1){ mergeSortRec(array, mid, tmp); mergeSortRec(array+mid, size-mid, tmp); mergeArrays(array, mid, array+mid, size-mid, tmp); for (i = 0; i < size; i++) array[i] = tmp[i]; } } Mergesort http://ecomputernotes.com
  • 27. 3 5 15 28 30 6 10 14 22 43 50 a: b: aSize: 5 bSize: 6 mergeArrays tmp: http://ecomputernotes.com
  • 28. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 j=0 3 6 http://ecomputernotes.com
  • 29. mergeArrays 5 15 28 30 10 14 22 43 50 a: b: tmp: i=0 k=0 3 j=0 3 6 http://ecomputernotes.com
  • 30. mergeArrays 3 15 28 30 10 14 22 43 50 a: b: tmp: i=1 j=0 k=1 3 5 5 6 http://ecomputernotes.com
  • 31. mergeArrays 3 5 28 30 10 14 22 43 50 a: b: 3 5 tmp: i=2 j=0 k=2 6 15 6 http://ecomputernotes.com
  • 32. mergeArrays 3 5 28 30 6 14 22 43 50 a: b: 3 5 6 tmp: i=2 j=1 k=3 15 10 10 http://ecomputernotes.com
  • 33. mergeArrays 10 3 5 28 30 6 22 43 50 a: b: 3 5 6 tmp: i=2 j=2 k=4 15 10 14 14 http://ecomputernotes.com
  • 34. mergeArrays 14 10 3 5 28 30 6 14 43 50 a: b: 3 5 6 tmp: i=2 j=3 k=5 15 10 22 15 http://ecomputernotes.com
  • 35. mergeArrays 14 10 3 5 30 6 14 43 50 a: b: 3 5 6 tmp: i=3 j=3 k=6 15 10 22 22 15 28 http://ecomputernotes.com
  • 36. mergeArrays 14 10 3 5 30 6 14 50 a: b: 3 5 6 tmp: i=3 j=4 k=7 15 10 22 28 15 28 43 22 http://ecomputernotes.com
  • 37. mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 tmp: i=4 j=4 k=8 15 10 22 30 15 28 43 22 30 28 http://ecomputernotes.com
  • 38. mergeArrays 14 10 3 5 6 14 50 a: b: 3 5 6 30 tmp: i=5 j=4 k=9 15 10 22 15 28 43 22 30 28 43 50 Done. http://ecomputernotes.com
  • 39. Merge Sort and Linked Lists Sort Sort Merge http://ecomputernotes.com
  • 40. Mergesort Analysis Merging the two lists of size n /2: O( n ) Merging the four lists of size n/4: O( n ) . . . Merging the n lists of size 1: O( n ) O (lg n ) times Mergesort is O( n lg n ) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging
  • 41. Mergesort Analysis Mergesort is O( n lg n ) Space? The other sorts we have looked at (insertion, selection) are in-place (only require a constant amount of extra space) Mergesort requires O( n ) extra space for merging http://ecomputernotes.com
  • 42. Quicksort Quicksort is another divide and conquer algorithm Quicksort is based on the idea of partitioning (splitting) the list around a pivot or split value http://ecomputernotes.com
  • 43. Quicksort First the list is partitioned around a pivot value. Pivot can be chosen from the beginning, end or middle of list): 8 3 2 11 7 5 4 10 12 4 5 5 pivot value http://ecomputernotes.com
  • 44. Quicksort The pivot is swapped to the last position and the remaining elements are compared starting at the ends. 8 3 2 11 7 5 4 10 12 4 5 low high 5 pivot value http://ecomputernotes.com
  • 45. Quicksort Then the low index moves right until it is at an element that is larger than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 10 12 4 6 low high 5 pivot value 3 12 http://ecomputernotes.com
  • 46. Quicksort Then the high index moves left until it is at an element that is smaller than the pivot value (i.e., it is on the wrong side) 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 http://ecomputernotes.com
  • 47. Quicksort Then the two values are swapped and the index values are updated: 8 6 2 11 7 5 4 10 12 4 6 low high 5 pivot value 3 2 12 http://ecomputernotes.com
  • 48. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 3 10 3 10 http://ecomputernotes.com
  • 49. Quicksort This continues until the two index values pass each other: 8 6 12 11 7 5 4 2 4 6 low high 5 pivot value 10 3 http://ecomputernotes.com
  • 50. Quicksort Then the pivot value is swapped into position: 8 6 12 11 7 5 4 2 4 6 low high 10 3 8 5 http://ecomputernotes.com
  • 51. Quicksort Recursively quicksort the two parts: 5 6 12 11 7 8 4 2 4 6 10 3 Quicksort the left part Quicksort the right part 5 http://ecomputernotes.com
  • 52. Quicksort void quickSort(int array[], int size) { int index; if (size > 1) { index = partition(array, size); quickSort(array, index); quickSort(array+index+1, size - index-1); } } http://ecomputernotes.com
  • 53. int partition(int array[], int size) { int k; int mid = size/2; int index = 0; swap(array, array+mid); for (k = 1; k < size; k++){ if (array[k] < array[0]){ index++; swap(array+k, array+index); } } swap(array, array+index); return index; } Quicksort
  • 54. Data Structures-Course Recap Arrays Link Lists Stacks Queues Binary Trees Sorting http://ecomputernotes.com

Editor's Notes

  • #5: End of lecture 44.
  • #6: Start of lecture 45
  • #53: End of lecture 45. At last!